#include <stdio.h>

typedef struct __account_t {
	pthread_mutex_t mut;
	double balance;
} account_t;

// 转账，从 src 账户往 target 账户上转 amt 元
void transfer(account_t *src, 
				account_t *target, 
				double amt) {
	// 锁定转出账户
	pthread_mutex_lock(&src->mut);
	// 锁定转入账户
	pthread_mutex_lock(&target->mut);
	if (src->balance > amt) {
    	src->balance -= amt;
		target->balance += amt;
    }
	pthread_mutex_unlock(&target->mut);
	pthread_mutex_unlock(&src->mut);
}


int main() {
	account_t src;
	src.balance = 100;
	
	pthread_mutex_init(&src.mut, NULL);


	account_t target;
    target.balance = 100;

	pthread_mutex_init(&target.mut, NULL);

	transfer(&src, &target, 20);

	printf("src 账户余额：%f\n", src.balance);	
	printf("target 账户余额：%f\n", target.balance);	
}
